package com.ztjava.ask.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.ztjava.ask.model.AskCatalog;
import com.ztjava.ask.service.AskService;
import com.ztjava.ask.validator.AddAnswerValidator;
import com.ztjava.ask.validator.AddQuestionValidator;
import com.ztjava.ask.validator.AddReplyValidator;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.utiliy.StringUtil;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;

public class AskBaseController extends BaseController{

	private static AskService askService = Duang.duang(AskService.class);
	/***
	 * 新建问题
	 */
	@UserMethod(option="ASK_AUTH")
	public void addQuestion(){
		keepPara("catalog_id");
		setAttr("catalogList", askService.treeCatalog(getParaMap()));
		render("addQuestion.html");
	}
	/***
	 * 回答问题
	 */
	@UserMethod(option="ASK_AUTH")
	@Before(AddQuestionValidator.class)
	public void saveQuestion(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.insertQuestion(getParaToInt("catalog_id"),  getPara("title"), getPara("content"), 
				StringUtil.multiple2(getParaValues("images")), getParaToInt("score",0),authSession.getId()));
	}
	/***
	 * 问题查看
	 */
	@UserMethod(option="ASK_AUTH")
	public void show(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		askService.showQuestion(this, getParaToInt("id"), authSession);
		setAttr("answerList", askService.getAnswerList(getParaToInt("id"), authSession));
		render(((AskCatalog)getAttr("askCatalog")).getStr("show_tpl"));
	}
	/***
	 * 回答问题
	 */
	@UserMethod(option="ASK_AUTH")
	@Before(AddAnswerValidator.class)
	public void saveAnswer(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.insertAnswer(getParaToInt("question_id"), getPara("content"), getPara("image"),authSession.getId()));
	}	
	/***
	 * 回复答案
	 */
	@UserMethod(option="ASK_AUTH")
	@Before(AddReplyValidator.class)
	public void saveReply(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.insertReply(getParaToInt("answer_id"),getPara("content"), getPara("image"), authSession.getId()));
	}
	/***
	 * 采纳答案
	 */
	@UserMethod(option="ASK_AUTH")
	public void good(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.goodAnswer(getParaToInt("answer_id"), authSession.getId()));
	}
	/***
	 * 赞答案
	 */
	@UserMethod(option="ASK_AUTH")
	public void ding(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.updateDing(getParaToInt("answer_id",0),authSession.getId()));
	}
	/***
	 * 收藏问题
	 */
	@UserMethod(option="ASK_AUTH")
	public void fav(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.updateFav(getParaToInt("question_id"), authSession.getId()));
	}	
}
